package com.wanpinghui.supplier.message.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.wphlibrary.model.db.bean.MessageReadBean;
import com.wanpinghui.wphlibrary.model.db.manager.ObjectDbManager;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBidNotiExt;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.util.List;

/***
 * 功能描述:竞价成功通知
 * 作者:qiujialiu
 * 时间:2017/12/8
 ***/

public class BidSuccMsgAdapter extends BaseRecyclerViewAdapter<MsgBean, BidSuccMsgAdapter.BidSuccHolder> {
    private ObjectDbManager objectDbManager;
    private ClickListener clickListener;

    public BidSuccMsgAdapter(Context context) {
        super(context);
        objectDbManager = ObjectDbManager.newInstance();
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void setDatas(List<MsgBean> datas) {
        super.setDatas(datas);

    }

    @Override
    public BidSuccHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bid_succ_msg, null);
        return new BidSuccHolder(view);
    }

    @Override
    public void onBindViewHolder(final BidSuccHolder holder, final int position) {
        final MsgBean msgBeanO = datas.get(position);
        final MsgBean msgBean = new Gson().fromJson(msgBeanO.getMessage().replaceAll(" ", ""), MsgBean.class);
        holder.textViewContent.setText(msgBean.getMessage());
        holder.textViewTime.setText(WTimeUtil.getChatTime(msgBeanO.getCreated_at()));
        if (objectDbManager.isMessageRead(Integer.toString(msgBeanO.getMessage_id()))) {
            holder.viewReaded.setVisibility(View.INVISIBLE);
        } else {
            holder.viewReaded.setVisibility(View.VISIBLE);
            objectDbManager.saveMessageRead(new MessageReadBean(Integer.toString(msgBeanO.getMessage_id())));
        }
        String extStr = msgBean.getExt();
        if (TextUtil.isNotEmpty(extStr)) {
            final MsgBidNotiExt ext = new Gson().fromJson(extStr, MsgBidNotiExt.class);
            //  final MsgBidNotiExt ext = JSONObject.parseObject(extStr,MsgBidNotiExt.class);
            holder.textViewDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    objectDbManager.saveMessageRead(new MessageReadBean(Integer.toString(msgBeanO.getMessage_id()), 1));
                    holder.viewReaded.setVisibility(View.INVISIBLE);
                    if (clickListener != null) {
                        clickListener.goDetail(ext.getDemand_id());
                    }
                }
            });
            final String stringPhone = ext.getMobile();

            String Message = msgBean.getMsg().replace("\n", "");
            if(TextUtil.isNotEmpty(stringPhone)){
                String stringPhone2 = stringPhone.substring(0, 3) + "-" + stringPhone.substring(3, 7) + "-" + stringPhone.substring(7);
                Message = Message.replace(stringPhone, stringPhone2);
                SpannableString spannableString = new SpannableString(Message);

                spannableString.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        if (clickListener != null) {
                            clickListener.callPhone(stringPhone);
                        }
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        ds.setUnderlineText(false);
                    }
                }, Message.indexOf(stringPhone2), Message.indexOf(stringPhone2) + 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimaryBlue))
                        , Message.indexOf(stringPhone2), Message.indexOf(stringPhone2) + 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.textViewContent.setText(spannableString);
                holder.textViewContent.setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class BidSuccHolder extends RecyclerView.ViewHolder {
        TextView textViewContent;
        TextView textViewTime;
        View viewReaded;
        TextView textViewDetail;

        public BidSuccHolder(View itemView) {
            super(itemView);
            textViewContent = (TextView) itemView.findViewById(R.id.tv_message);
            textViewTime = (TextView) itemView.findViewById(R.id.tv_time);
            viewReaded = itemView.findViewById(R.id.view_readed);
            textViewDetail = (TextView) itemView.findViewById(R.id.tv_detail);

            itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    public interface ClickListener {
        void goDetail(long demandId);

        void callPhone(String phone);
    }
}
